#include "DoxyFile.h"

#include <QtXml>

DoxyFile::DoxyFile(QString refId)
{
    int pos  = refId.lastIndexOf("/");
    m_refId = refId.mid(pos+1);
    m_xmlFileName = refId.append(".xml");
}

/**
 * @brief loadFile Loads the file
 * @return True if the file was processed correctly
 */
bool DoxyFile::loadFile()
{        QDomDocument document;
    bool fileLoaded = false;

    QFile* file = new QFile(m_xmlFileName);

    if(file->open(QIODevice::ReadOnly))
    {


        if(document.setContent(file))
        {
            file->close();

            QDomElement docElem = document.documentElement();

            QDomNode root = docElem.firstChild();

            while(!root.isNull())
            {
                QDomElement element  = root.toElement();

                if(!element.isNull())
                {
                    //double check that we have the correct file
                    if(element.attribute("id") == m_refId)
                    {
                        fileLoaded = true;

                        QDomNodeList list = element.childNodes();

                        for(int i =0; i < list.size(); i++)
                        {
                            QDomElement subElement = list.at(i).toElement();
                            if(!subElement.isNull())
                            {
                                if(subElement.tagName() == "includes")
                                {
                                    QString localInclude = subElement.attribute("local");
                                    if(localInclude == "no")
                                    {
                                        m_externalIncludes.push_back(subElement.text());
                                    }
                                    else if(localInclude == "yes")
                                    {
                                        RefItem ref;
                                        ref.refId = subElement.attribute("refid");
                                        ref.name = subElement.text();
                                        m_localIncludes.push_back(ref);
                                    }
                                }
                                else if(subElement.tagName() == "includedby")
                                {
                                    QString localInclude = subElement.attribute("local");
                                    //we only care if this file is included by another local file
                                    if(localInclude == "yes")
                                    {
                                        RefItem ref;
                                        ref.refId = subElement.attribute("refid");
                                        ref.name = subElement.text();
                                        m_includeBy.push_back(ref);
                                    }
                                }
                                else if (subElement.tagName() == "location")
                                {
                                    m_fileName = subElement.attribute("file");
                                }
                            }
                        }
                    }
                }
                root = root.nextSibling();
            }
        }
    }
return fileLoaded;
}

/**
* @brief getFileName Get the name of the file as represented by the file
* @return The file name including it's path
*/
QString DoxyFile::getFileName() const
{
    return m_fileName;
}

QString DoxyFile::getDoxRef() const
{
    return m_refId;
}

/**
* @brief getIncludedBy Gets all the files that also include this file
* @param files
*/
void DoxyFile::getIncludedBy(QList<RefItem>& files) const
{
    files = m_includeBy;
}


/**
* @brief getLocalIncludes Gets all the files that have been included by this class, that have also been processed by doxygen
* @param files A list that will be populated with all of the local file includes
*/
void DoxyFile::getLocalIncludes(QList<RefItem>& files) const
{
    files = m_localIncludes;
}


/**
* @brief getExternalIncludes Gets all the files that are included by this class but have not been processed by doxygen
* @param files A list of external to doxygen file includes.
*/
void DoxyFile::getExternalIncludes(QStringList& files) const
{
    files = m_externalIncludes;
}
